#include "../header/cpu/cpu_info.h"
#include "../header/utils/utils.h"


void get_cpu_occupy_info(CPU_OCCUPY* cpu_occupy){
    FILE *fp;
    fp = fopen("/proc/stat","r");
    if(fp != NULL){
        fseek(fp,0,SEEK_SET);
        char buff[1024];
        fgets(buff,1024,fp);
        sscanf(buff,"%s %u %u %u %u %u %u %u",cpu_occupy->name,&cpu_occupy->user,&cpu_occupy->nice,&cpu_occupy->system,&cpu_occupy->idle,&cpu_occupy->iowait,&cpu_occupy->irq,&cpu_occupy->softirq);
        fclose(fp);
    }
}
//o
double  calc_cpu_occupy(CPU_OCCUPY* o,CPU_OCCUPY* n){
    double od, nd;
    double id, sd;
    double cpu_use ;

    od = (double) (o->user + o->nice + o->system +o->idle+o->softirq+o->iowait+o->irq);//第一次(用户+优先级+系统+空闲)的时间再赋给od
    nd = (double) (n->user + n->nice + n->system +n->idle+n->softirq+n->iowait+n->irq);//第二次(用户+优先级+系统+空闲)的时间再赋给od

    id = (double) (n->idle); //用户第一次和第二次的时间之差再赋给id
    sd = (double) (o->idle);//系统第一次和第二次的时间之差再赋给sd
    if((nd-od) != 0)
    cpu_use =100.0 - ((id-sd))/(nd-od)*100.00;
    else
    cpu_use = 0;
    return cpu_use;
}
//获取CPU使用率
double get_cpu_usage(){
    CPU_OCCUPY od,no;
    get_cpu_occupy_info(&od);
    double cpu_usage;
    //printf("get_cpu_usage:%s %u %u %u %u %u %u %u",od.name,od.user,od.nice,od.system,od.idle,od.iowait,od.irq,od.softirq);
    sleep(1);
    get_cpu_occupy_info(&no);
    cpu_usage = calc_cpu_occupy(&od,&no);
    //printf("cpu_usage:%.2f\n",cpu_usage);
    return cpu_usage;
}
//获取CPU核心数
int get_cpu_cores(){
    return sysconf(_SC_NPROCESSORS_ONLN);
}

//获取操作系统发行版
void sys_release(char* os_name_version){
    FILE* fp;
    fp = fopen("/etc/os-release","r");
    char release_buff[256];
    char sys_release_name[50];
    char sys_name[50];
    char sys_version[50];
    memset(release_buff,'\0',256);
    if(fp != NULL){
        while(fgets(release_buff,256,fp)){
            if(indexOf(release_buff,PRETTY_NAME)==0){
                substring(sys_release_name,release_buff,strlen(PRETTY_NAME)+2,strlen(release_buff)-2);
                break;
            }
            if(indexOf(release_buff,NAME)==0){
                substring(sys_name,release_buff,strlen(NAME)+1,strlen(release_buff)-2);
            }
            if(indexOf(release_buff,VERSION)==0){
                substring(sys_version,release_buff,strlen(VERSION)+1,strlen(release_buff)-2);
            }
        }
        fclose(fp);
        if(strlen(sys_release_name)>0){
            strcpy(os_name_version,sys_release_name);
            return;
        }
        if(strlen(sys_name)>0){
            sprintf(os_name_version,"%s%s",sys_name,sys_version);
            return ;
        }

    }

    fp = fopen("/etc/system-release","r");
    memset(release_buff,'\0',256);
    if(fp != NULL){
        fgets(release_buff,256,fp);
        fclose(fp);
        strcpy(os_name_version,release_buff);
        return ;
    }
    char file_name[100];
    find_release_file(file_name);
    if(strlen(file_name)>0){
        fp = fopen(file_name,"r");
        memset(release_buff,'\0',256);
        if(fp != NULL){
            fgets(release_buff,256,fp);
            fclose(fp);
            strcpy(os_name_version,release_buff);
        return ;
        }
    }
    struct utsname buf;
	if(uname(&buf)<0){
		perror("uname");
		return ;
	}

	sprintf(os_name_version,"%s %s %s %s",buf.sysname,buf.release,buf.version,buf.machine);
}

//获取/etc/*-release文件
void  find_release_file(char* file_name){
    DIR* dir;
    struct dirent* entry;
    dir = opendir("/etc");
    while((entry = readdir(dir))){
        if(strstr(entry->d_name,"-release")!=NULL&&strcmp(entry->d_name,"system-release")!=0&&strcmp(entry->d_name,"os-release")!=0){
            int idx = indexOf(entry->d_name,"-");
            int lst = lastIndexOf(entry->d_name,"-");
            if(idx==lst)
                strcpy(file_name,entry->d_name);
        }
    }

}

//获取开机时间
void sys_start_time(char* start_time,struct sysinfo* info){
	long uptime = 0;
	if(0==sysinfo(info)){
		uptime = info->uptime;
	}
	time_t time_p;
	struct tm *p;
	time(&time_p);
	time_p -= uptime;
	p=localtime(&time_p);
	char ctime[256] = {'\0'};
	strftime(ctime,sizeof(ctime),"%Y-%m-%d %H:%M:%S",p);
	strcpy(start_time,ctime);
}

